
#ifndef PacketHead_h__
#define PacketHead_h__

#include "stdint.h"
#include "ByteBuffer.h"

enum DataEncodeType
{
	DATAENCODE_PLAIN = 0,
	DATAENCODE_XOR = 1,
	DATAENCODE_AES = 2,
	DATAENCODE_RSA = 3,
	DATAENCODE_RSA_PLAIN = 4,
	DATAENCODE_RSA_XOR = 5,
	DATAENCODE_RSA_AES = 6,
};

#pragma pack(1)
class C2SPacketHead
{
public:
	C2SPacketHead()
	{
		memset(this, 0x00, sizeof(*this));
	}
public:
	uint16_t headId;
	uint16_t dataType;
	uint32_t dataId;
	uint32_t dataLen;
	uint16_t checksum;
	uint16_t reserved;

public:
	size_t decode(const char * buffer, size_t size)
	{
		size_t typeSize = sizeof(*this);
		if (size < typeSize)
		{
			return 0;
		}
		memcpy(this, buffer, typeSize);

		return typeSize;
	}

	size_t encode(char *buffer, size_t size)
	{
		size_t typeSize = sizeof(*this);
		if (size < sizeof(*this))
		{
			return 0 ;
		}

		memcpy(buffer, this, typeSize);
		return typeSize;
	}

	DataEncodeType encodeType()
	{
		return DataEncodeType(checksum & 0x0F);
	}
};

class S2SPacketHead
{
public:
	S2SPacketHead()
	{
		memset(this, 0x00, sizeof(*this));
	}
public:
	uint32_t session_id;
	uint32_t body_len;
	uint32_t req_id;
	uint16_t protocol_id;

public:
	int decode(ByteBuffer &reader)
	{
		size_t typeSize = sizeof(*this);
		if (size < typeSize)
		{
			return 0;
		}

		reader >> session_id;
		reader >> body_len;
		reader >> req_id;
		reader >> protocol_id;

		return typeSize;
	}

	int encode(ByteBuffer &writer)
	{
		size_t typeSize = sizeof(*this);
		if (size < sizeof(*this))
		{
			return 0;
		}

		writer << session_id;
		writer << body_len;
		writer << req_id;
		writer << protocol_id;

		return typeSize;
	}
};

#pragma pop()

#endif // PacketHead_h__
